//
//  HWMyShangPingViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/23.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWMyShangPingViewController.h"
#import "HWShiWuCollectionViewCell.h"
#import "HWGoodsCollectionCell.h"
#import "HWshangpingshoucangModel.h"
#import "HWShangpingxiangqingViewController.h"


@interface HWMyShangPingViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;


@property(strong,nonatomic) UIImageView *defaultIamegView;
@property(strong,nonatomic) UILabel *wuLabel;

@end

@implementation HWMyShangPingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   [self setupUI];
    
    [self zanWuNoDdataIcoImageView];
    
}


- (void)setupUI {
    
     self.view.backgroundColor = RGB(255, 255, 255);
    
      [self addControlsview];
      [self addrefreshControl];
    
      
    
}




- (void)zanWuNoDdataIcoImageView {
    
    self.defaultIamegView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-189)/2,(kScreenH-NavBarHeight - 127)/2-15-80, 189, 124)];
    self.defaultIamegView.image = [UIImage imageNamed:@"details_dealbg_NOdata"];
    [self.maincollectionView addSubview: self.defaultIamegView];
    
    
    self.wuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.defaultIamegView.bottom, kScreenW, 15)];
    self.wuLabel.text = @"暂无数据";
    self.wuLabel.font = [UIFont systemFontOfSize:13];
    self.wuLabel.textColor = RGB(102, 102, 102);
    self.wuLabel.textAlignment  = NSTextAlignmentCenter;
    [self.maincollectionView addSubview: self.wuLabel];
    

}




#pragma  mark ---- 添加刷新

- (void)addrefreshControl {

    self.maincollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];

    [self.maincollectionView.mj_header beginRefreshing];

    self.maincollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {



    self.p = @"1";

    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];

    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"1"];

    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];

    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"mycollect",
                                       @"uid" :uidStr,
                                       @"t" : tStr,
                                       @"type" :typeStr,
                                       @"sign" : singMD5
                                       };

       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {

           // [SVProgressHUD dismiss];
           [self.maincollectionView.mj_header endRefreshing];
           [self.maincollectionView.mj_footer endRefreshing];


           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

           if ([codeStr intValue] == 1) {
               
               
               self.defaultIamegView.hidden = YES;
               self.wuLabel.hidden = YES;
               

              self.dataMuArr = [HWshangpingshoucangModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];

             


           } else if ([codeStr intValue] == 0) {
               
               
               self.defaultIamegView.hidden = NO;
               self.wuLabel.hidden = NO;
                             
               
           }
           [self.maincollectionView reloadData];


       } failure:^(NSString *error) {
           [self.maincollectionView.mj_header endRefreshing];
           [self.maincollectionView.mj_footer endRefreshing];


       }];


}

// 上拉加载

- (void)onRefresh {



    int p = [self.p intValue];

    int max_p = [self.max_p intValue];

     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
       [self.maincollectionView.mj_header endRefreshing];
                [self.maincollectionView.mj_footer endRefreshing];
                            
        return ;

      }



     NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];

     NSString *typeStr =  [NSString stringWithFormat:@"%@",@"1"];

     NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];

     NSString *singMD5 = [WMUtil md5HexDigest:singStr];


     NSDictionary *parametes = @{
                                         @"p" :self.p,
                                         @"ios" : @"1",
                                         @"ct" :  @"app",
                                         @"ac" : @"mycollect",
                                         @"uid" :uidStr,
                                         @"t" : tStr,
                                         @"type" :typeStr,
                                         @"sign" : singMD5
                                         };

        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {

             // [SVProgressHUD dismiss];
             [self.maincollectionView.mj_header endRefreshing];
             [self.maincollectionView.mj_footer endRefreshing];


             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

             if ([codeStr intValue] == 1) {




                 NSMutableArray *mutableArr = [HWshangpingshoucangModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataMuArr addObject:mutableArr[i]];

                 }

                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.maincollectionView reloadData];


             }



         } failure:^(NSString *error) {
             [self.maincollectionView.mj_header endRefreshing];
             [self.maincollectionView.mj_footer endRefreshing];


         }];




}




#pragma  mark -----  添加控件
- (void)addControlsview {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    
 //   CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
    layout.minimumInteritemSpacing = 15;
    layout.minimumLineSpacing = 12;
  //  layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-NavBarHeight - 127) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
  //  self.maincollectionView.backgroundColor = [UIColor whiteColor];
    
  //  self.maincollectionView.backgroundColor =RGB(255, 255, 255);
   // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 12, 0, 12);
    
  //  self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
  //  [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
    [self.maincollectionView registerClass:[HWGoodsCollectionCell class] forCellWithReuseIdentifier:@"cellID"];
  //  [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    
    self.maincollectionView.backgroundColor  = [UIColor whiteColor];
    
    
     self.maincollectionView.userInteractionEnabled = YES;
    [self.view  addSubview:self.maincollectionView];
    
    
    
}



#pragma mark --------- UICollectionViewDelegate


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
 
    return self.dataMuArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    HWShiWuCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
//    cell.backgroundColor = [UIColor whiteColor];
    
    
   // HWGoodsCollectionCell
    
    HWGoodsCollectionCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    cell.backgroundColor = [UIColor whiteColor];
   // cell.backgroundColor = [UIColor redColor];
    cell.model = self.dataMuArr[indexPath.row];
    
    
    [cell.quxiaoBut addTarget:self action:@selector(quxiaoButTap:) forControlEvents:UIControlEventTouchUpInside];
    
    
     cell.layer.shadowColor = [UIColor colorWithRed:186.0/255.0 green:186.0/255.0 blue:186.0/255.0 alpha:0.2].CGColor;
     cell.layer.shadowOffset = CGSizeMake(0,1);
     cell.layer.shadowOpacity = 1;
     cell.layer.shadowRadius = 10;
     cell.clipsToBounds = NO;
      return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((SCREEN_WIDTH-15-12*2)/2,174);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
    
//    HWShangpingxiangqingViewController *shangping = [[HWShangpingxiangqingViewController alloc]init];
//    [self.navigationController pushViewController:shangping animated:YES];
    HWshangpingshoucangModel *mdel = self.dataMuArr[indexPath.row];
    HWShangpingxiangqingViewController *Shangpingxiangqing = [[HWShangpingxiangqingViewController alloc]init];
    Shangpingxiangqing.goodsID =mdel.goods_id;
    [self.navigationController pushViewController:Shangpingxiangqing animated:YES];
    
    
    
}

// 取消收藏
- (void)quxiaoButTap:(UIButton *)sender {

    NSLog(@"%@",[[sender superview] superview]);
    
    HWGoodsCollectionCell *cell = (HWGoodsCollectionCell *)[[sender superview] superview];
     NSIndexPath *index =   [self.maincollectionView indexPathForCell:cell];
     HWshangpingshoucangModel *mdel = self.dataMuArr[index.row];
     
      int  r =  [WMUtil getRandomNumber:101 to:998];
      NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
       
      NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],r];
      NSString *typeStr = [NSString stringWithFormat:@"%@",@"1"];
     
      NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];

      NSString *singMD5 = [WMUtil md5HexDigest:singStr];


      NSDictionary *parametes = @{
    
                                          @"ios" : @"1",
                                          @"ct" :  @"app",
                                          @"ac" : @"shoucang_del",
                                          @"uid" :uidStr,
                                          @"t" : tStr,
                                          @"type" :typeStr,
                                          @"sign" : singMD5,
                                          @"id" : mdel.id
                                          };

         [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {

         


              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

              if ([codeStr intValue] == 1) {


                  [MBProgressHUD showSuccess:@"取消收藏成功"];
                   [self.maincollectionView.mj_header beginRefreshing];


              }else {
                  
                  
                   [MBProgressHUD showError:response[@"message"]];
                  
              }



          } failure:^(NSString *error) {
              [MBProgressHUD showError:@"收藏失败"];

          }];

    
    
    
    
    
    
    
}


@end
